package com.hx.dc.controllor;

import com.hx.dc.entity.*;
import com.hx.dc.service.*;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.relation.Role;
import java.util.*;
import java.util.List;

@Controller
@RequestMapping("role")
public class RoleAction extends BaseAction {

	@Autowired
	private RoleCatagoryService roleCatagoryService;

	@Autowired
	private DcEmployeeService dcEmployeeService;

	@Autowired
	private DcRoleService dcRoleService;

	@Autowired
	private DcCatagotyService dcCatagotyService;

	@RequestMapping(value = "/selectPage")
	@ResponseBody
	// easyui提交数据：page:1 rows:15
	public Object selectPage(DcRole dcRole, Integer page, Integer rows) {
		System.out.println("page:" + page + ",rows:" + rows);
		return this.getPage(dcRoleService.selectPage(dcRole, page, rows));
	}

	@RequestMapping(value = "/roleadd")
	@ResponseBody
	// easyui提交数据：page:1 rows:15
	public Object addRole(DcRole dcRole) {
		dcRoleService.insert(dcRole);
		return null;
	}

	@RequestMapping(value = "/roleupdate")
	@ResponseBody
	// easyui提交数据：page:1 rows:15
	public Object updateRole(DcRole dcRole) {
		dcRoleService.update(dcRole);
		return null;
	}

	@RequestMapping(value = "/roledel")
	@ResponseBody
	// easyui提交数据：page:1 rows:15
	public Object delRole(Integer id,DcRole dcRole) {
		dcRole.setRoleId(id);
		dcRoleService.delete(dcRole);
		return null;
	}
	@RequestMapping(value = "/rolesetright")
	@ResponseBody
	// easyui提交数据：page:1 rows:15
	public void setrightRole(Integer rows, String name, DcRole dcRole) {
		dcRole.setRoleName(rows);
		dcRoleService.deleteCataRole(dcRole);

		try {
			ObjectMapper objectMapper = new ObjectMapper();
			Integer[] integers = null;

			//把json格式的字符串转为java对象数组
			integers = objectMapper.readValue(name, Integer[].class);


			//Arrays.asList把普通JAVA数组转为list对象
			List<RoleCatagory> list= new ArrayList<RoleCatagory>();

			for (int i=0;i<integers.length;i++){
				RoleCatagory roleCatagory=new RoleCatagory();
				roleCatagory.setCatagoryRight(integers[i]);
				roleCatagory.setRoleName(rows);
				list.add(roleCatagory);
			}

			int i=roleCatagoryService.insertRoleCatagoryList(list);
			System.out.println(i);
		} catch (Exception e){
			e.printStackTrace();
		}
	}


	@RequestMapping(value = "/rolesetemp")
	@ResponseBody
	// easyui提交数据：page:1 rows:15
	public Object setempRole(Integer emprole,String empcode,DcEmployee dcEmployee) {
		dcEmployee.setEmployeeStatus(emprole);
		dcEmployee.setUsercode(empcode);
		dcEmployeeService.update(dcEmployee);
		return null;
	}
}
